/*********************************************************
 * copyright xinbohit.com 版权所有 
 * 开发人员：IvanYu
 * 创建时间：2014/11/4 20:33:36
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LEARNREN.COM.TOOL;
using LEARNREN.COM.BLL;
using LEARNREN.COM.Entity;
using System.Web.Script.Serialization;

namespace LEARNREN.COM.WebSite.Admin
{
    /// <summary>
    /// BannerHandler 的摘要说明
    /// </summary>
    public class BannerHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string falseStr = "{{\"result\":false,\"msg\":\"{0}\"}}";
            string trueStr = "{\"result\":true}";

            string operation = context.Request.Form["o"];
            string idStr = context.Request.Form["id"];
            long id;
            string title = context.Request.Form["title"];
            string url = context.Request.Form["url"];
            string orderStr = context.Request.Form["order"];
            int order = 0;
            string groupIdStr = context.Request.Form["groupId"];
            long groupId;
            string image = context.Request.Form["image"];

            // 0:添加，1：修改，2：删除,3:根据Id查询
            switch (operation)
            {
                case "0":
                    if (string.IsNullOrEmpty(title))
                    {
                        context.Response.Write(string.Format(falseStr, "标题不能为空。"));
                    }
                    else if (string.IsNullOrEmpty(url))
                    {
                        context.Response.Write(string.Format(falseStr, "目标网址不能为空。"));
                    }
                    else if (!int.TryParse(orderStr,out order))
                    {
                        context.Response.Write(string.Format(falseStr, "排序必须填写整数。"));
                    }
                    else if (!long.TryParse(groupIdStr, out groupId))
                    {
                        context.Response.Write(string.Format(falseStr, "请选择正确的分组。"));
                    }
                    else if (string.IsNullOrEmpty(image))
                    {
                        context.Response.Write(string.Format(falseStr, "必须上传一张图片。"));
                    }
                    else
                    {
                        if (Insert(title, url, order, groupId, image))
                        {
                            context.Response.Write(trueStr);
                        }
                        else
                        {
                            context.Response.Write(string.Format(falseStr, "系统内部错误，插入失败。"));
                        }
                    }
                    break;
                case "1":
                    if (!long.TryParse(idStr, out id))
                    {
                        context.Response.Write(string.Format(falseStr, "id格式不正确。"));
                    }
                    if (string.IsNullOrEmpty(title))
                    {
                        context.Response.Write(string.Format(falseStr, "标题不能为空。"));
                    }
                    else if (string.IsNullOrEmpty(url))
                    {
                        context.Response.Write(string.Format(falseStr, "目标网址不能为空。"));
                    }
                    else if (!int.TryParse(orderStr, out order))
                    {
                        context.Response.Write(string.Format(falseStr, "排序必须填写整数。"));
                    }
                    else if (!long.TryParse(groupIdStr, out groupId))
                    {
                        context.Response.Write(string.Format(falseStr, "请选择正确的分组。"));
                    }
                    else
                    {
                        if (Edit(id, title, url, order, groupId, image))
                        {
                            context.Response.Write(trueStr);
                        }
                        else
                        {
                            context.Response.Write(string.Format(falseStr, "系统内部错误，修改失败。"));
                        }
                    }
                    break;
                case "2":
                    if (!long.TryParse(idStr, out id))
                    {
                        context.Response.Write(string.Format(falseStr, "id格式不正确。"));
                    }
                    else
                    {
                        if (Delete(id))
                        {
                            context.Response.Write(trueStr);
                        }
                        else
                        {
                            context.Response.Write(string.Format(falseStr, "系统内部错误，删除失败。"));
                        }
                    }
                    break;
                case "3":
                    if (!long.TryParse(idStr, out id))
                    {
                        context.Response.Write(string.Format(falseStr, "id格式不正确。"));
                    }
                    else
                    {
                        try
                        {
                            using (BannerLogic logic = new BannerLogic())
                            {
                                Banner cs = logic.GetById(id);
                                if (cs != null)
                                {
                                    var o = new { State = true, Data = cs };
                                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                                    string os = serializer.Serialize(o);
                                    context.Response.Write(os);
                                }
                                else
                                {
                                    var o = new { State = false, Message = "没有找到ID为"+id+"的幻灯片。" };
                                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                                    string os = serializer.Serialize(o);
                                    context.Response.Write(os);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Loger.LogErr(ex);
                            context.Response.Write(string.Format(falseStr, "系统内部错误，删除失败。"));
                        }
                    }
                    break;
                default:
                    context.Response.Write(falseStr);
                    break;
            }
        }

        private bool Edit(long id, string title, string url, int order, long groupId, string image)
        {
            try
            {
                using (BannerLogic logic = new BannerLogic())
                {
                    var obj = logic.GetById(id);
                    obj.Title = title;
                    obj.Link = url;
                    obj.Order = order;
                    obj.GroupID = groupId;
                    if (!string.IsNullOrEmpty(image))
                    {
                        obj.Image = image;
                    }
                    logic.Save(obj);
                }
                return true;
            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
                return false;
            }
        }

        private bool Insert(string title, string url, int order, long groupId, string image)
        {
            try
            {
                Banner obj = new Banner();
                obj.Title = title;
                obj.Link = url;
                obj.Order = order;
                obj.GroupID = groupId;
                obj.Image = image;
                obj.CreatedTime = DateTime.Now;
                using (BannerLogic logic = new BannerLogic())
                {
                    obj.Id = logic.GetNewIdentity();
                    logic.Save(obj);
                }
                return true;
            }
            catch (Exception e)
            {
                Loger.LogErr(e);
                return false;
            }
        }

        private bool Delete(long id)
        {
            try
            {
                using (BannerLogic logic = new BannerLogic())
                {
                    var obj = logic.GetById(id);
                    if (obj != null)
                        logic.Delete(obj);
                }
                return true;
            }
            catch (Exception ex)
            {
                Loger.LogErr(ex);
                return false;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}